package com.habdqn.attendance.web.admin;

import com.alibaba.fastjson.JSON;
import com.habdqn.attendance.dto.LayUIData;
import com.habdqn.attendance.entity.Department;
import com.habdqn.attendance.service.DepartmentService;
import com.habdqn.attendance.service.impl.DepartmentServiceImpl;
import lombok.extern.log4j.Log4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author XingJiejian
 * @date 2019/10/25 0025
 */
@Log4j
@WebServlet(name = "DeptDetailServlet", urlPatterns = "/admin/dept/detail")
public class DeptDetailServlet extends HttpServlet {
    /**
     * 处理新增修改表单
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数，绑定对象
        String strId = request.getParameter("id");
        String name = request.getParameter("name");
        Department department = new Department();
        if (strId != null && !"".equals(strId)) {
            department.setId(Integer.parseInt(strId));
        }
        department.setName(name);

        //执行更新、保存操作
        DepartmentService service = new DepartmentServiceImpl();
        department = service.update(department);

        // 响应结果
        LayUIData data = new LayUIData(200, "success", 0, department);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSON.toJSONString(data));
    }

    /**
     * 进入新增/修改页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        //修改页面
        if (strId != null && !"".equals(strId)) {
            int id = Integer.parseInt(strId);
            DepartmentService departmentService = new DepartmentServiceImpl();
            Department department = departmentService.queryById(id);
            request.setAttribute("dept", department);
        }
        request.getRequestDispatcher("/WEB-INF/jsp/admin/dept/detail.jsp").forward(request, response);
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        DepartmentService departmentService = new DepartmentServiceImpl();
        boolean flag = false;
        // 删除单个部门
        String strId = request.getParameter("id");
        if (strId != null && !"".equals(strId)) {
            int id = Integer.parseInt(strId);
            flag = departmentService.deleteById(id);
        }

        // 批量删除部门
        String ids = request.getParameter("ids");
        if (ids != null && !"".equals(ids)) {
            flag = departmentService.batchDeleteByIds(ids);
        }
        LayUIData data = new LayUIData(200, String.valueOf(flag), 0, null);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSON.toJSONString(data));
    }
}
